import clientEnum from "./client.enum"

export default class SphereSource {
  id: any
  updateSourceCall: any
  userData: any
  volumeCache: { origin: { x: number; y: number; z: number }; radius: number }
  constructor(t, e, s, i) {
    this.id = t
    this.updateSourceCall = e
    this.userData = s
    this.volumeCache = { origin: { x: 0, y: 0, z: 0 }, radius: 1 / 0 }
    this.updateCache(i.origin, i.radius)
  }
  get facade() {
    const t = { radius: this.volumeCache.radius, origin: Object.assign({}, this.volumeCache.origin) }
    const e = this,
      s = new (class SphereFacade {
        get type() {
          return clientEnum.ct.SPHERE
        }
        get volume() {
          return t
        }
        get userData() {
          return e.userData
        }
        commit() {
          e.commit(this)
        }
      })()
    return clientEnum.ot.map.set(s, this), s
  }
  commit(t) {
    this.hasSourceChanged(t) && (this.updateCache(t.volume.origin, t.volume.radius), this.updateSourceCall(this.id, clientEnum.ct.SPHERE, this.volumeCache))
  }
  hasSourceChanged(t) {
    return !(
      this.volumeCache.origin.x === t.volume.origin.x &&
      this.volumeCache.origin.y === t.volume.origin.y &&
      this.volumeCache.origin.z === t.volume.origin.z &&
      this.volumeCache.radius === t.volume.radius
    )
  }
  updateCache(t, e) {
    ;(this.volumeCache.origin.x = t.x), (this.volumeCache.origin.y = t.y), (this.volumeCache.origin.z = t.z), (this.volumeCache.radius = e)
  }
}
